package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.utils.RequestPermission;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IRoleService roleService ;

    @Autowired
    private IEmployeeService employeeService ;

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    @RequestPermission(name="员工查询",expression = "employee:list")
    public String list(Model model , @ModelAttribute("qo") EmployeeQueryObject qo){
        Page<Object> objects = PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);


        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments);
        return "employee/list" ;
    }

    @RequestMapping("/delete")
    @RequestPermission(name="员工删除",expression = "employee:delete")
    public String delete(Long id){
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list" ;
    }


    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments);
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles",roles);
        if (id != null) {
            model.addAttribute("employee",employeeService.selectById(id));
            model.addAttribute("ownRoles",roleService.selectOwnRole(id));
        }
        return "/employee/input" ;
    }

    @RequestMapping("/checkUserName")
    @ResponseBody
    public Map<String,Boolean> checkUserName(String username){
        boolean exits = employeeService.checkedUsername(username);
        Map<String,Boolean> data = new HashMap<>();
        data.put("valid",!exits);
        return data ;
    }

    @RequestMapping("/save")
    @RequestPermission(name="员工编辑",expression = "employee:save")
    @ResponseBody
    public JsonResult save(Employee employee , Long[] ids) {


        try {
            if (employee.getId() != null) {
                employeeService.update(employee,ids);
            } else {
                employeeService.insert(employee, ids);
            }
            return new JsonResult(true,"编辑成功");
        }catch (Exception e){
            return new JsonResult(false,"编辑失败");
        }

    }

    @RequestMapping("/exportXls")
    public void exportXml(HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");

        Workbook wb = employeeService.exportXls() ;

        wb.write(response.getOutputStream());

    }

    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        Workbook wb =new HSSFWorkbook(file.getInputStream());

        employeeService.importXls(wb);

        return "redirect:/employee/list";
    }
}
